<template>
  <div>
    <!-- 搜索 -->
    <TopFilter
      ref="topFilter1"
      :list="topFilterInit"
      :cols="3"
      labelWidth="100"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>
    <!-- :params="flterValue" -->
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :isSelectColumn="isSelectColumn"
      :params="flterValue"
      :fetchapi="fetchApi"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
      columnsRef="projectOperating_projram_push_index_01"
    >
      <template slot="controls">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="drawerHandle(null,'add')"
        >新增</el-button>
      </template>
    </FilterTable>
    <!-- 选择车型 -->
    <BaseDialog
      title="选择车型"
      :visible.sync="visibleCarInfo"
      width="700px"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <carInfoDlg
        ref="carInfoDlg"
        selectType="single"
        :rowData2="formData2"
        @close="closeTypeCarDialog"
      />
    </BaseDialog>
    <!-- 方案新增，修改 -->
    <Drawer
      :visible.sync="visibleDrawer"
      title="项目方案信息"
      :width="920"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <info-modal ref="InfoRef" v-model="initData" :rowData="formData" @close="drawerClose" />
    </Drawer>
    <!-- 方案明细 -->
    <Drawer
      :visible.sync="visibleDetailDrawer"
      title="方案明细"
      :width="920"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <detail v-model="initData" @close="detailClose" />
    </Drawer>
  </div>
</template>

<script>
// import typeCar from '../commonForm/infoActivityTypeCar'
//从
// import { addCarInfo } from '@/api/customerManage/continueManage/record/index';

// import carInfoDlg from '@/pages/cms-common/product'; //car选择
import carInfoDlg from '@/pages/cms-common/model'; //car选择
// import CarInfo from './infoCar'; //意向车型需求信息
import infoModal from './info.vue';
import detail from '@/pages/cms-pages/projectOperating/program/oemInquire/detail.vue';
import { dictionary } from '@/utils/dictMixin';
import { getData, selectListByBill } from '@/api/cms-api/projectOperating/program/push';
import { formatdateByDay } from '@/utils';

export default {
  mixins: [dictionary],
  components: {
    infoModal,
    carInfoDlg,
    detail
    // ,typeCar
  },
  data() {
    return {
      v_modelId: '',
      fetchParams: getData, //ipa 接口
      visibleCarInfo: false,
      visibleDrawer: false,
      visibleDetailDrawer: false, //明细页面
      isSelectColumn: false,
      topFilterInit: this.topFilterInitFun(),
      flterValue: { programIssuedChannel: 25301001,programStatusList:'25281001,25281002',programStatus:25281001}, //参数
      initData: {},
      formData2: {},
      formData: {},
      columns: this.creatHeader(),
      fetchApi: getData //ipa 接口
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  //方法、事件
  methods: {
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: '方案编号', fieldName: 'programCode', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '方案名称', fieldName: 'programName', placeholder: '请输入', initialValue: '' },
        { type: 'SELECT', label: '方案状态', fieldName: 'programStatus', placeholder: '请选择', initialValue: 25281001, itemList: this.createDictList('2528',25281003) },
        { type: 'RANGE_DATE', label: '方案下发日期', fieldName: 'start_end_time', valueFormat: 'yyyy-MM-dd', initialValue: '', placeholder: '请选择' },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelName',
          placeholder: '请选择',
          initialValue: '',
          readonly:true,
          unitRender: () => {
            return (
                <div>
                  <el-button slot="append" icon="el-icon-search" onClick={() =>this.openModelDialog()}></el-button>
                  <el-button slot="append" icon="el-icon-delete" onClick={() => this.clearSelectModel()}></el-button>
                </div>
            );
          }
        },
        { type: 'INPUT', label: '项目名称', fieldName: 'itemName', placeholder: '请输入', initialValue: '' },
        { type: 'SELECT', label: '是否全网下发', fieldName: 'isNetworkIssued', placeholder: '请选择', initialValue: '', itemList: this.createDictList('1004') }
      ];
    }, //搜索获取数据
    changeHandle(val) {
      // debugger;
      if (!val.modelName || typeof val.modelName == 'undefined' || val.modelName == '') {
        this.v_modelId = '';
      }
      let extParams = { modelId: this.v_modelId, programIssuedChannel: 25301001 };
      if (val && val.start_end_time && typeof val.start_end_time != 'undefined' && val.start_end_time.length > 0) {
        extParams.issuedTimeSearchStart = val.start_end_time[0];
        extParams.issuedTimeSearchEnd = val.start_end_time[1];
      }
      this.flterValue = Object.assign({}, val, extParams,{programStatusList:'25281001,25281002'});
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            //已作废和已下发的方案不能再编辑
            let disabled = props.row.programStatus == 25281002 || props.row.programStatus == 25281003 ? true : false;
            return (
              <div>
                <el-button size="mini" type="text" disabled={disabled} onClick={() => this.drawerHandle(props.row, 'edit')}>
                  编辑
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" onClick={() => this.drawerDetailHandle(props.row, 'show')}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        // 首页列表头信息
        {
          title: '方案状态',
          dataIndex: 'programStatus',
          sorter: true,
          render: props => {
            //编码转汉字
            return <span>{this.createDictText(props.row.programStatus, '2528')}</span>;
          }
        },
        { title: '方案编号', dataIndex: 'programCode', sorter: true },
        { title: '方案名称', dataIndex: 'programName', sorter: true },
        {
          title: '项目周期',
          dataIndex: 'projectStartDate',
          sorter: true,
          render: props => {
            return (
              <span>
                {formatdateByDay(props.row.projectStartDate)}-{formatdateByDay(props.row.projectEndDate)}
              </span>
            );
          }
        },
        { title: '创建人', dataIndex: 'createdByName', sorter: true },
        { title: '创建时间', dataIndex: 'createdAt', dateFormat: 'yyyy-MM-dd', sorter: true },
        { title: '下发人', dataIndex: 'issuedPersonName', sorter: true },
        { title: '下发时间', dataIndex: 'issuedTime', dateFormat: 'yyyy-MM-dd', sorter: true }
      ];
    },
    openModelDialog() {
      this.visibleCarInfo = true;
    },
    closeTypeCarDialog(data) {
      this.visibleCarInfo = false; //关闭弹窗
      //返回数据逻辑自己写
      this.fetchParams = { ...this.fetchParams };
      // this.fetchParams.modelId=data.data[0].modelId;
      // console.log('项目下发首页1:', data.data[0].modelName, this.$refs.topFilter1.list[4].fieldName, data.data, this.fetchParams);
      var name = data.data[0].modelName;

      // this.$refs.topFilter1.list[4].initialValue = data.data[0].modelName; //给车型赋值
      this.v_modelId = data.data[0].modelId;

      this.topFilterInit.find(item => item.fieldName === 'modelName').initialValue= data.data[0].modelName; //给车型赋值
    },
    //清空选择车型
    clearSelectModel(){
        this.topFilterInit.find(item => item.fieldName === 'modelName').initialValue='';
        this.v_modelId = '';
    },
    //方案详情
    async drawerDetailHandle(row, type) {
      let pram = {
        type: 'show', //add/edit/show
        rowData: row,
        rowData2: row
      };
      let res = {};
      this.formData = row;
      this.formData.type = type;
      let fileParams = { projectProgramId: this.formData.projectProgramId, billType: 25281001 };
      res = await selectListByBill(fileParams);
      // debugger
      if (res.data.length > 0) {
        let caseUrlList = [];
        for (let j = 0; j < res.data.length; j++) {
          let o1 = { name: res.data[j].fileName };
          let o2 = { url: res.data[j].fileId };
          let a = Object.assign(o1, o2);
          caseUrlList.push(a);
        }
        this.formData = Object.assign({}, { fileUrl: caseUrlList }, this.formData);
        pram.rowData = this.formData;
      }
      // this.initData = pram;
      // this.formData2 = pram;
      this.initData = pram;
      this.formData2 = pram;
      this.visibleDetailDrawer = true;
    },
    //关闭详情
    detailClose(params) {
      this.visibleDetailDrawer = false;
      if (params.refresh == true) {
        this.flterValue = { ...this.filterValue };
      }
    },
    //新增修改
    async drawerHandle(row, type) {
      let pram = {
        type: type, //add/edit/show
        rowData: row,
        rowData2: row
      };
      // 上传文件
      // debugger
      if (type === 'edit' || type === 'show') {
        let res = {};
        this.formData = row;
        this.formData.type = type;
        let fileParams = { projectProgramId: this.formData.projectProgramId, billType: 25281001 };
        res = await selectListByBill(fileParams);
        // debugger
        if (res.data.length > 0) {
          let caseUrlList = [];
          for (let j = 0; j < res.data.length; j++) {
            let o1 = { name: res.data[j].fileName };
            let o2 = { url: res.data[j].fileId };
            let a = Object.assign(o1, o2);
            caseUrlList.push(a);
          }
          this.formData = Object.assign({}, { fileUrl: caseUrlList }, this.formData);
          pram.rowData = this.formData;
        }
        // this.initData = pram;
        // this.formData2 = pram;
        this.initData = pram;
        this.formData2 = pram;
      } else {
        type = 'add';
        this.formData = pram;
        this.initData = pram;
        //   this.formData = null;
        // this.initData = null;
        // this.formData2 = null;
        // this.formData2 = pram;
      }

      this.visibleDrawer = true;
    },
    //关闭详情
    drawerClose(val, type) {
      // debugger

      this.visibleDrawer = false;
      if (type === 'refresh') {
        // 重新加载
        this.flterValue = { ...this.flterValue };
      }
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    }
  }
};
</script>

<style scoped>
</style>
